 <template>
   <view style="width: 100%;height: 100%;">
     <view class="root" :style="{display: !visibleCamera ? 'flex': 'none'}">
       <view class="content">
         <uni-forms ref="formRef" :model="form" validateTrigger="blur" label-position="top" label-width="100%"
           err-show-type="toast">
           <view class="content-wrapper">
             <yy-title title="电动车/摩托车"></yy-title>
             <view class="content-inner-wrapper">
               <uni-forms-item label="车架编号：" name="vehicleVin" required key="vehicleVin">
                 <view class="form-content-item" :class="form.vehicleVin ?  '' : 'input-item'">
                   <uni-easyinput type="text" :maxlength="30" v-model.trim="form.vehicleVin" :inputBorder="false"
                     placeholder="请填写" :clearable="true" />
                 </view>
               </uni-forms-item>
               <uni-forms-item label="车牌号：" name="vehicleNumber" key="vehicleNumber">
                 <view class="form-content-item" :class="form.vehicleNumber ?  '' : 'input-item'"
                   @click="handleShowCar">
                   <uni-easyinput type="text" disabled v-model.trim="form.vehicleNumber" :inputBorder="false"
                     placeholder="请选择" :clearable="true" />
                 </view>
               </uni-forms-item>
               <uni-forms-item label="品牌型号：" name="vehicleModule" required key="vehicleModule">
                 <view class="form-content-item" :class="form.vehicleModule ?  '' : 'input-item'">
                   <uni-easyinput type="text" :maxlength="30" v-model.trim="form.vehicleModule" :inputBorder="false"
                     placeholder="请填写" :clearable="true" />
                 </view>
               </uni-forms-item>
               <uni-forms-item label="电动车图片:(最多可上传3张图片，非必填)" name="imgList" style="margin-bottom: 0;">
                 <u-upload :fileList="form.imgList" @afterRead="(info) => afterReadFive(info, 'form', 75)"
                   @delete="(info) => deletePhoto(info, 'form')" name="imgList" :maxCount="3" :multiple="true"
                   width="96" height="80">
                   <image :src="baseImgUrl + 'upload-icon3.png'" style="width: 200rpx; height: 160rpx"></image>
                 </u-upload>
               </uni-forms-item>
             </view>
           </view>
           <view class="content-wrapper">
             <yy-title title="物品所属人信息"></yy-title>
             <uni-forms-item label="身份证号:" name="belongingPersonIdcard" required key="belongingPersonIdcard">
               <view class="form-content-item" :class="form.belongingPersonIdcard ?  '' : 'input-item'">
                 <uni-easyinput type="idcard" :maxlength="18" v-model.trim="form.belongingPersonIdcard"
                   :inputBorder="false" placeholder="请填写" :clearable="true" />
                 <u-icon slot="right" size="21" name="scan" color="#2488F5" @click="handleCamera"></u-icon>
               </view>
             </uni-forms-item>
             <uni-forms-item label="姓名:" name="belongingPersonName" required key="belongingPersonName">
               <view class="form-content-item" :class="form.belongingPersonName ?  '' : 'input-item'">
                 <uni-easyinput type="text" :maxlength="30" v-model.trim="form.belongingPersonName" :inputBorder="false"
                   placeholder="请填写" :clearable="true" />
               </view>
             </uni-forms-item>
             <uni-forms-item label="联系电话:" name="belongingPersonMobile" required key="belongingPersonMobile">
               <view class="form-content-item" :class="form.belongingPersonMobile ?  '' : 'input-item'">
                 <uni-easyinput type="number" :maxlength="11" v-model.trim="form.belongingPersonMobile"
                   :inputBorder="false" placeholder="请填写" :clearable="true" />
               </view>
             </uni-forms-item>
           </view>
         </uni-forms>
       </view>
       <view class="bottom">
         <yy-button-submit @submit="onSubmit"></yy-button-submit>
       </view>
     </view>
     <u-keyboard @backspace="backspace" autoChange safeAreaInsetBottom @cancel="showKeyBoard = false"
       @confirm="showKeyBoard = false" ref="uKeyboard" mode="car" :show="showKeyBoard" @change="valChange"></u-keyboard>
     <yy-photo-mask :visible.sync="visibleCamera" :baseImgUrl="baseImgUrl" :clientId="clientId"
       :clientSecret="clientSecret" @parsedSuccess="onParsedSuccess"></yy-photo-mask>
   </view>
 </template>

 <script>
   import {
     electricvehiclesSave
   } from '../api/api'
   import uploadFileMixin from '../utils/upload-file-mixin'
   import {
     Toast,
     getOptionsParams,
     idCardRule,
     isLoading,
     navBack,
     phoneRule
   } from '../utils/util'
   export default {
     mixins: [uploadFileMixin],
     data() {
       return {
         baseImgUrl: this.$baseImgUrl,
         routerParams: {},
         clientId: this.$clientId,
         clientSecret: this.$clientSecret,
         visibleCamera: false,
         form: this.initForm(),
         showKeyBoard: false,
         rules: {
           vehicleVin: {
             rules: [{
               required: true,
               errorMessage: "请填写车架编号！",
             }, {
               pattern: '^[0-9A-Z]+$',
               errorMessage: "车架编号只能输入数字和大写字母！"
             }]
           },
           vehicleNumber: {
             rules: [{
               required: false,
               errorMessage: "请填写车牌号！",
             }]
           },
           vehicleModule: {
             rules: [{
               required: true,
               errorMessage: "请填写品牌型号！",
             }]
           },
           imgList: {
             rules: [{
               required: false,
               errorMessage: "请上传电动车图片！",
             }]
           },
           belongingPersonIdcard: {
             rules: idCardRule()
           },
           belongingPersonName: {
             rules: [{
               required: true,
               errorMessage: "请填写姓名！",
             }]
           },
           belongingPersonMobile: {
             rules: phoneRule()
           },
         },
         uKeyboard: null
       }
     },
     onLoad(options) {
       this.routerParams = getOptionsParams(options)
     },
     onReady() {
       this.$refs.formRef.setRules(this.rules)
     },
     methods: {
       initForm() {
         return {
           vehicleVin: '',
           vehicleNumber: '', // 车牌号
           vehicleModule: '',
           belongingPersonIdcard: '',
           belongingPersonName: '',
           belongingPersonMobile: '',
           imgList: []
         }
       },
       onSubmit() {
         this.$refs.formRef.validate().then(() => {
             let data = {
               companyMessageId: this.routerParams.companyMessageId,
               ...this.form,
             };

             // 处理图片信息
             if (data.imgList) {
               data.imgList = data.imgList.map(item => {
                 return item.halfPath
               })
             }
             isLoading(true)
             electricvehiclesSave(data).then((res) => {
                 if (res.code == 200) {
                   setTimeout(() => {
                     navBack();
                   }, 100)
                   uni.$emit('register-load')
                 }
                 Toast(res.message);
               })
               .finally(() => {
                 isLoading(false)
               })
           })
           .catch(() => {})
       },
       handleCamera() {
         this.visibleCamera = true;
       },
       onParsedSuccess(value) {
         const {
           domicileAddress,
           userName,
           idcard,
           nation
         } = value;
         this.$set(this.form, "belongingPersonName", userName);
         this.$set(this.form, "belongingPersonIdcard", idcard);
         this.visibleCamera = false;
       },
       handleShowCar() {
         this.showKeyBoard = true
       },
       // 按键被点击(点击退格键不会触发此事件)
       valChange(val) {
         // 将每次按键的值拼接到value变量中，注意+=写法
         if (this.form.vehicleNumber.length >= 15) {
           Toast('车牌号最多输入15位！')
           return
         }
         this.form.vehicleNumber += val;
       },
       // 退格键被点击
       backspace() {
         // 删除value的最后一个字符
         if (this.form.vehicleNumber.length) {
           this.form.vehicleNumber = this.form.vehicleNumber.substr(0, this.form
             .vehicleNumber.length - 1)
           if (this.form.vehicleNumber.length === 0) {
             // 没有 length 的时候
             // 切换成中文
             this.$refs.uKeyboard.$children[0].$children[2].abc = false
           }
         }
       }
     }
   }
 </script>

 <style lang="scss" scoped>
   @import '@/styles/uni-form-label-top.scss';

   /deep/.u-keyboard__button--center {
     .u-keyboard__button__inner-wrapper:first-of-type {
       display: none;
     }
   }

   .root {
     display: flex;
     width: 100%;
     height: 100%;
     flex-direction: column;
     background: #F9F9F9;

     .content {
       flex: 1;
       height: 0;
       overflow: auto;

       .content-wrapper {
         background: #FFFFFF;
         box-shadow: 0rpx 3rpx 6rpx 1rpx rgba(207, 207, 207, 0.1608);
         border-radius: 8rpx 8rpx 8rpx 8rpx;
         padding: 40rpx 20rpx;
         margin-bottom: 10rpx;
       }

       .pawn-list-wrapper {
         background: #F9F9F9;
         padding: 25rpx;
         margin-bottom: 10rpx;

         .title-name {
           display: flex;
           justify-content: space-between;
           margin-bottom: 23rpx;

           .name-i {
             font-weight: bold;
             font-size: 32rpx;
             color: #181818;
           }

           .delete-i {
             display: flex;
             align-items: center;
             font-weight: 500;
             font-size: 26rpx;
             color: #F94A4A;
             flex-shrink: 0;

             .text-delete {
               margin-left: 10rpx;
             }
           }
         }

         .i-item {
           margin-bottom: 15rpx;
           display: flex;

           .i-label {
             font-weight: 500;
             font-size: 28rpx;
             color: #999999;
             width: 119rpx;
             margin-right: 16rpx;
             position: relative;
             text-align: justify;
             text-align-last: justify;
             flex-shrink: 0;



             &::after {
               content: ':';
               position: absolute;
               right: -8rpx;
             }
           }

           .i-value {
             font-weight: bold;
             font-size: 28rpx;
             color: #101010;
           }

         }
       }

       .datetime-picker-wrapper {
         display: flex;
         align-items: center;
         justify-content: space-between;

         .text-t {
           font-weight: 500;
           font-size: 30rpx;
           color: #666666;
         }

         .t-item {
           font-weight: 400;
           font-size: 28rpx;
           color: #A2A2A2;
           width: 304rpx;
           height: 70rpx;
           background: #F9F9F9;
           border-radius: 8rpx 8rpx 8rpx 8rpx;
           border: 1rpx solid #F1EEEE;
           display: flex;
           align-items: center;
           justify-content: center;
         }

         .has-value {
           font-weight: bold;
           font-size: 30rpx;
           color: #181818;
         }
       }
     }
   }
 </style>